package com.yz.sharkim.generator;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.junit.Ignore;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * Mysql 表反向映射实体类
 * @author yangzheng
 * @version 1.0
 * @date 2019/11/15
 */
@Ignore
public class SharkImGenerator {

    private String[] includes = {
            "t_user", "t_group", "t_message_temp"
    };

    private final String SAVE_PATH = System.getProperty("user.dir") + "/src/main/java";


    private GlobalConfig config = new GlobalConfig().setIdType(IdType.AUTO)
                                        .setBaseResultMap(true).setOutputDir(SAVE_PATH)
                                        .setAuthor("yangzheng").setOpen(false);

    private DataSourceConfig dataSourceConfig = new DataSourceConfig()
            .setUrl("jdbc:mysql://192.168.99.100:3306/shark_im?useUnicode=true&useSSL=false&characterEncoding=utf8")
            .setDriverName("com.mysql.cj.jdbc.Driver")
            .setUsername("root")
            .setPassword("123456");

    @Test
    void codeGenerator() {
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();
        mpg.setGlobalConfig(config);
        mpg.setDataSource(dataSourceConfig);

        mpg.setPackageInfo(new PackageConfig()
                .setModuleName("generator")
                .setParent("com.yz.sharkim"));

        // 自定义配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {

            }
        };


        // 自定义输出配置
        cfg.setFileOutConfigList(new ArrayList<>());
        mpg.setCfg(cfg);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true);
        strategy.setEntitySerialVersionUID(true);
        strategy.setRestControllerStyle(true);
        strategy.setEntityColumnConstant(true);
        strategy.setControllerMappingHyphenStyle(true);
        strategy.setTablePrefix("t_");
        strategy.setInclude(includes);
        strategy.setFieldPrefix("is_");
        // 自动填充
        strategy.setTableFillList(
                Arrays.asList(new TableFill("update_time", FieldFill.INSERT_UPDATE),
                        new TableFill("create_time", FieldFill.INSERT))
        );
        mpg.setStrategy(strategy);
        mpg.execute();
    }

}
